package com.whut.www.file_storage.activity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.whut.www.file_storage.CustomCursorAdapter;
import com.whut.www.file_storage.CustomSQLiteOpenHelper;
import com.whut.www.file_storage.R;

public class ViewBinderActivity extends AppCompatActivity {
    //数据库名
    private static final String DB_NAME = "db_words.db";
    //数据库版本
    private static final int DB_VERSION = 1;
    //表名
    private static final String tableName = "tb_words";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_binder);
        TextView tv_empty = (TextView) findViewById(R.id.tv_empty);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setEmptyView(tv_empty);
        CustomSQLiteOpenHelper sqLiteOpenHelper = new CustomSQLiteOpenHelper(this, DB_NAME, null,
                DB_VERSION);
        SQLiteDatabase sqLiteDatabase = sqLiteOpenHelper.getWritableDatabase();
        Cursor cursor = sqLiteDatabase.query(tableName, null, null, null, null, null, null);
        //setSimpleCursorAdapter(listView, cursor);//通过ViewBinder动态的修改某一列的显示样式
        CustomCursorAdapter cursorAdapter = new CustomCursorAdapter(this, cursor);
        listView.setAdapter(cursorAdapter);
    }

    private void setSimpleCursorAdapter(ListView listView, Cursor cursor) {
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.item_listview,
                cursor, new String[]{"word", "detail"}, new int[]{R.id.tv_itemSummary, R.id
                .tv_itemDetail}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        listView.setAdapter(cursorAdapter);
        //动态的修改某一列的显示样式
        cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            //用来给某个控件绑定数据
            //true:由我们自己来处理数据的绑定.
            //false:由系统本身来处理数据的绑定.
            /**
             * @param   view--->当前绘制的视图
             * @param   cursor--->绘制当前视图所需的cursor数据
             * @param   columnIndex--->绘制当前视图时所使用的数据在cursor中列的下标
             */
            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                if (cursor.getColumnIndex("detail") == columnIndex && view instanceof TextView) {
                    TextView textView = (TextView) view;
                    textView.setTextSize(30);
                    textView.setTextColor(Color.YELLOW);
                    String detail = cursor.getString(columnIndex);
                    textView.setText("123");
                    //此处表示,该事件的绑定由我自己来处理
                    return true;
                }
                return false;
            }
        });
    }
}
